package cn.school.workflow.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.school.common.utils.SysUserUtils;
import cn.school.workflow.dao.MeetingPersonDao;
import cn.school.workflow.dao.MeetingReservedApplyDao;
import cn.school.workflow.dao.MeetingSuppliesDao;
import cn.school.workflow.service.MeetingReservedApplyService;
import cn.school.workflow.service.WorkFlowCommonService;
import cn.school.workflow.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MeetingReservedApplyServiceImpl extends ServiceImpl<MeetingReservedApplyDao, MeetingReservedApplyVO> implements MeetingReservedApplyService {

    @Resource
    private WorkFlowCommonService workFlowCommonService;
    @Resource
    private MeetingPersonDao meetingPersonDao;
    @Resource
    private MeetingSuppliesDao meetingSuppliesDao;
    @Resource
    private MeetingReservedApplyDao meetingReservedApplyDao;

    @Override
    public IPage<MeetingReservedApplyVO> findMeetingApplyPageList(MeetingReservedApplyVO vo) {
        int count =  this.baseMapper.findMeetingApplyPageListCount(vo);
        int startNum=(vo.getPageNum()-1)*vo.getPageSize();
        vo.setPageNum(startNum);
        List<MeetingReservedApplyVO> list =  this.baseMapper.findMeetingApplyPageList(vo);
        IPage<MeetingReservedApplyVO> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public boolean cancelMeetingApply(WorkFlowVO vo) {
        LambdaQueryWrapper<MeetingReservedApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MeetingReservedApplyVO::getProcessId,vo.getProcessInstanceId());
        MeetingReservedApplyVO applyVO = new MeetingReservedApplyVO();
        applyVO.setApplyStatus("2");//撤销流程
        this.baseMapper.update(applyVO,wrapper);
        return workFlowCommonService.cancelApply(vo.getProcessInstanceId(),vo.getApplyDesc());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createMeetingApply(MeetingReservedApplyVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();
        //获取用户下级审批人
//        String assignee = workFlowCommonService.findAssigneeList();
        String applyNo = DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");
        vo.setApplyNo(applyNo);
        vo.setCreateBy(username);
        vo.setCreateTime(new Date());
        //创建申请单
        this.baseMapper.insert(vo);
        //添加与会人
        for (MeetingPersonVO personVO:vo.getPersonList()){
            personVO.setMeetingReservedId(vo.getId());
            personVO.setCreateBy(username);
            personVO.setCreateTime(new Date());
            meetingPersonDao.insert(personVO);
        }
        //添加会议物资
        for(MeetingSuppliesVO suppliesVO: vo.getSuppliesList()){
            suppliesVO.setMeetingReservedId(vo.getId());
            suppliesVO.setCreateBy(username);
            suppliesVO.setCreateTime(new Date());
            meetingSuppliesDao.insert(suppliesVO);
        }

        WorkFlowVO flowVO = new WorkFlowVO();
        flowVO.setApplyTitle("会议预定流程");
        flowVO.setApplyID("meetingApply");
        flowVO.setBusinessKey(applyNo);
        //查询下级处理人 TODO
        Map<String,Object> map =new HashMap<>();
        map.put("assignee","test1");
        flowVO.setVariables(map);
        //创建请假工作流
        String processInstanceId = workFlowCommonService.createProcessInstance(flowVO);
        vo.setProcessId(processInstanceId);
        //更新流程实例ID
        return this.baseMapper.updateById(vo);
    }

    @Override
    public MeetingReservedApplyVO findMeetingApplyDetail(Long id) {
        LambdaQueryWrapper<MeetingReservedApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MeetingReservedApplyVO::getProcessId,id);
        MeetingReservedApplyVO vo = this.baseMapper.selectOne(wrapper);

        LambdaQueryWrapper<MeetingPersonVO> personWrapper = new LambdaQueryWrapper<>();
        personWrapper.eq(MeetingPersonVO::getMeetingReservedId,vo.getId());
        vo.setPersonList(meetingPersonDao.selectList(personWrapper));

        LambdaQueryWrapper<MeetingSuppliesVO> suppliesWrapper = new LambdaQueryWrapper<>();
        suppliesWrapper.eq(MeetingSuppliesVO::getMeetingReservedId,vo.getId());
        vo.setSuppliesList(meetingSuppliesDao.selectList(suppliesWrapper));

        return vo;
    }
}
